package com.suyang.syt.service.impl;

import java.io.InputStream;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import com.suyang.syt.auth.AuthUtil;
import com.suyang.syt.dao.interfaces.BannerDao;
import com.suyang.syt.dao.interfaces.BugTypeDao;
import com.suyang.syt.dao.interfaces.CitysDao;
import com.suyang.syt.dao.interfaces.GroupDao;
import com.suyang.syt.dao.interfaces.PicDao;
import com.suyang.syt.dao.interfaces.RoleDao;
import com.suyang.syt.dao.interfaces.SystemNavigationDao;
import com.suyang.syt.dao.interfaces.UserDao;
import com.suyang.syt.dao.interfaces.UserGroupDao;
import com.suyang.syt.dao.interfaces.UserRoleDao;
import com.suyang.syt.model.Banner;
import com.suyang.syt.model.BugType;
import com.suyang.syt.model.Citys;
import com.suyang.syt.model.Group;
import com.suyang.syt.model.Pic;
import com.suyang.syt.model.Role;
import com.suyang.syt.model.SystemNavigation;
import com.suyang.syt.model.User;
import com.suyang.syt.model.UserGroup;
import com.suyang.syt.model.UserRole;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.service.interfaces.InitService;
import com.suyang.syt.util.DateFormat;
import com.suyang.syt.util.Md5Util;
import com.suyang.syt.util.PinyinUtil;

@Service
public class InitServiceImpl implements InitService {
	@Autowired
	private UserDao userDao;
	@Autowired
	private GroupDao groupDao;
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private UserGroupDao userGroupDao;
	@Autowired
	private UserRoleDao userRoleDao;
	@Autowired
	private SystemNavigationDao systemNavigationDao;
	@Autowired
	private CitysDao citysDao;
	@Autowired
	private BugTypeDao bugTypeDao;
	@Autowired
	private BannerDao bannerDao;
	@Autowired
	private PicDao picDao;

	@Override
	public AjaxObj addInit(String method) throws Exception {
		if ("user".equals(method)) {
			// 1、初始化用户
			return addUser();
		} else if ("nav".equals(method)) {
			// 2、初始化系统导航菜单
			return addSystemNavigation();
		} else if ("xzqy".equals(method)) {
			// 3、初始化行政区域,暂时只有河南
			return addGBCitys();
		} else if ("bug".equals(method)) {
			// 4、初始化BUG管里
			return addBugs();
		} else if ("weixin".equals(method)) {
			// 5、初始化微信
			return null;
		} else if ("init".equals(method)) {
			AjaxObj ajaxObj = addGBCitys();
			if (ajaxObj != null && ajaxObj.getResult() == 1) {
				ajaxObj = addBugs();
				if (ajaxObj != null && ajaxObj.getResult() == 1) {
					ajaxObj = addUser();
					if (ajaxObj != null && ajaxObj.getResult() == 1) {
						return ajaxObj;
					}
				}
			}
			return ajaxObj;
		}
		return new AjaxObj(0, "初始化失败！");
	}

	/** 初始化用户 */
	private AjaxObj addUser() throws Exception {
		String createTime = DateFormat.dateToString(new Date());

		// 1、添加用户
		User user = new User();
		user.setUsername("admin");
		user.setEmail("admin@qq.com");
		user.setNickname("超级管理员");
		user.setPassword(Md5Util.md5("admin", "123"));
		user.setStatus(1);
		user.setCreateTime(createTime);
		userDao.add(user);

		// 2、添加组
		Group group = new Group();
		group.setName("超级管理员");
		group.setDescribes("系统后台管理者的组");
		group.setCreateTime(createTime);
		groupDao.add(group);

		Group pxjgGroup = new Group();
		pxjgGroup.setName("培训机构组");
		pxjgGroup.setDescribes("培训机构组");
		pxjgGroup.setCreateTime(createTime);
		groupDao.add(pxjgGroup);

		Group jgjsGroup = new Group();
		jgjsGroup.setName("机构教师组");
		jgjsGroup.setDescribes("机构教师组");
		jgjsGroup.setCreateTime(createTime);
		groupDao.add(jgjsGroup);

		Group parentGroup = new Group();
		parentGroup.setName("家长组");
		parentGroup.setDescribes("家长组");
		parentGroup.setCreateTime(createTime);
		groupDao.add(parentGroup);

		// 3、添加超级管理员角色
		Role role = new Role();
		role.setName("超级管理员");
		role.setDescribes("拥有一切权限");
		role.setRoleType(AuthUtil.ROLE_ADMIN);
		roleDao.add(role);

		Role pxjgRole = new Role();
		pxjgRole.setName("培训机构");
		pxjgRole.setDescribes("拥有培训机构权限");
		pxjgRole.setRoleType(AuthUtil.ROLE_PXJG);
		roleDao.add(pxjgRole);

		Role jgjsRole = new Role();
		jgjsRole.setName("机构教师");
		jgjsRole.setDescribes("拥有机构教师权限");
		jgjsRole.setRoleType(AuthUtil.ROLE_JGJS);
		roleDao.add(jgjsRole);

		Role parentRole = new Role();
		parentRole.setName("家长");
		parentRole.setDescribes("拥有家长权限");
		parentRole.setRoleType(0);
		roleDao.add(parentRole);

		Role adminRole = new Role();
		adminRole.setName("管理员");
		adminRole.setDescribes("拥有一切权限");
		adminRole.setRoleType(AuthUtil.ROLE_ADMIN);
		roleDao.add(adminRole);

		// 5、设置组关联
		userGroupDao.add(new UserGroup(user.getId(), group.getId()));
		userRoleDao.add(new UserRole(user.getId(), role.getId()));

		return new AjaxObj(1, "用户信息初始化成功！");
	}

	/** 初始化菜单 */
	private AjaxObj addSystemNavigation() throws Exception {
		SystemNavigation s1 = new SystemNavigation();
		s1.setChecked("false");
		s1.setState("closed");
		s1.setText("导航");
		s1.setIconCls("icon-chart_organisation");
		systemNavigationDao.add(s1);

		SystemNavigation s11 = new SystemNavigation();
		s11.setChecked("false");
		s11.setState("closed");
		s11.setText("系统");
		s11.setPid(s1.getId());
		s11.setPtext(s1.getText());
		s11.setIconCls("icon-group_gear");
		systemNavigationDao.add(s11);

		SystemNavigation s111 = new SystemNavigation();
		s111.setChecked("false");
		s111.setState("open");
		s111.setText("用户管理");
		s111.setPid(s11.getId());
		s111.setPtext(s11.getText());
		s111.setUrl("/admin/user/list");
		s111.setIconCls("icon-user_edit");
		systemNavigationDao.add(s111);

		SystemNavigation s112 = new SystemNavigation();
		s112.setChecked("false");
		s112.setState("open");
		s112.setText("组管理");
		s112.setPid(s11.getId());
		s112.setPtext(s11.getText());
		s112.setUrl("/admin/group/list");
		s112.setIconCls("icon-group_add");
		systemNavigationDao.add(s112);

		SystemNavigation s113 = new SystemNavigation();
		s113.setChecked("false");
		s113.setState("open");
		s113.setText("角色管理");
		s113.setPid(s11.getId());
		s113.setPtext(s11.getText());
		s113.setUrl("/admin/role/list");
		s113.setIconCls("icon-tux");
		systemNavigationDao.add(s113);

		SystemNavigation s114 = new SystemNavigation();
		s114.setChecked("false");
		s114.setState("open");
		s114.setText("菜单管理");
		s114.setPid(s11.getId());
		s114.setPtext(s11.getText());
		s114.setUrl("/admin/systemNavigation/list");
		s114.setIconCls("icon-layout_content");
		systemNavigationDao.add(s114);

		SystemNavigation s115 = new SystemNavigation();
		s115.setChecked("false");
		s115.setState("open");
		s115.setText("日志管理");
		s115.setPid(s11.getId());
		s115.setPtext(s11.getText());
		s115.setUrl("/admin/operateLog/list");
		s115.setIconCls("icon-building_edit");
		systemNavigationDao.add(s115);

		SystemNavigation s116 = new SystemNavigation();
		s116.setChecked("false");
		s116.setState("open");
		s116.setText("行政区域");
		s116.setPid(s11.getId());
		s116.setPtext(s11.getText());
		s116.setUrl("/admin/citys/list");
		s116.setIconCls("icon-package_green");
		systemNavigationDao.add(s116);

		SystemNavigation s117 = new SystemNavigation();
		s117.setChecked("false");
		s117.setState("open");
		s117.setText("数据字典");
		s117.setPid(s11.getId());
		s117.setPtext(s11.getText());
		s117.setUrl("/admin/dictionary/list");
		s117.setIconCls("icon-table");
		systemNavigationDao.add(s117);

		SystemNavigation s118 = new SystemNavigation();
		s118.setChecked("false");
		s118.setState("open");
		s118.setText("Bug管理");
		s118.setPid(s11.getId());
		s118.setPtext(s11.getText());
		s118.setUrl("/admin/bug/list");
		s118.setIconCls("icon-bug");
		systemNavigationDao.add(s118);

		SystemNavigation s119 = new SystemNavigation();
		s119.setChecked("false");
		s119.setState("open");
		s119.setText("消息推送");
		s119.setPid(s11.getId());
		s119.setPtext(s11.getText());
		s119.setUrl("/admin/messageSpread/list");
		s119.setIconCls("icon-page_white_text");
		systemNavigationDao.add(s119);

		return new AjaxObj(1, "系统导航菜单初始化完毕！");
	}

	/** 初始化行政区域 */
	private AjaxObj addGBCitys() throws Exception {
		String createTime = DateFormat.dateToString(new Date());

		Citys root = new Citys();
		root.setCode("86");
		root.setText("中国");
		root.setJianpin("ZG");
		root.setQuanpin("ZhongGuo");
		root.setState("closed");
		citysDao.add(root);

		InputStream xml = this.getClass().getClassLoader().getResourceAsStream("henan_city.xml");
		Citys citys = null;
		Citys child = null;
		Citys grandson = null;
		String code = "";
		String text = "";
		String node = "";
		String layer = "";
		XmlPullParser pullParser = XmlPullParserFactory.newInstance().newPullParser();
		pullParser.setInput(xml, "UTF-8");
		int event = pullParser.getEventType();
		while (event != XmlPullParser.END_DOCUMENT) {
			switch (event) {
			case XmlPullParser.START_DOCUMENT:
				break;
			case XmlPullParser.START_TAG:
				code = pullParser.getAttributeValue(0);
				text = pullParser.getAttributeValue(1);
				layer = pullParser.getAttributeValue(2);
				if ("province".equals(pullParser.getName())) {
					citys = new Citys();
					citys.setCode(code);
					citys.setText(text);
					citys.setQuanpin((PinyinUtil.str2Pinyin(text, null)));
					citys.setJianpin(PinyinUtil.strFirst2Pinyin(text).toUpperCase());
					citys.setState("closed");
					citys.setPid(root.getId());
					citys.setPtext(root.getText());
					citys.setLayer(Integer.parseInt(layer));
					citysDao.add(citys);
				} else if ("city".equals(pullParser.getName())) {
					child = new Citys();
					child.setCode(code);
					child.setText(text);
					child.setQuanpin((PinyinUtil.str2Pinyin(text, null)));
					child.setJianpin(PinyinUtil.strFirst2Pinyin(text).toUpperCase());
					node = pullParser.getText();
					if (node != null && "/>".equals(node.substring(node.length() - 2, node.length()))) {
						child.setState("open");
					} else {
						child.setState("closed");
					}
					child.setPid(citys.getId());
					child.setPtext(citys.getText());
					child.setLayer(Integer.parseInt(layer));
					citysDao.add(child);
				} else if ("county".equals(pullParser.getName())) {
					grandson = new Citys();
					grandson.setCode(code);
					grandson.setText(text);
					grandson.setQuanpin((PinyinUtil.str2Pinyin(text, null)));
					grandson.setJianpin(PinyinUtil.strFirst2Pinyin(text).toUpperCase());
					grandson.setState("open");
					grandson.setPid(child.getId());
					grandson.setPtext(child.getText());
					grandson.setLayer(Integer.parseInt(layer));
					citysDao.add(grandson);
				}
				break;
			case XmlPullParser.END_TAG:
				if ("province".equals(pullParser.getName())) {
					citys = null;
				} else if ("city".equals(pullParser.getName())) {
					child = null;
				} else if ("county".equals(pullParser.getName())) {
					grandson = null;
				}
				break;
			}
			event = pullParser.next();
		}

		// 添加默认城市(郑州)的默认首页banner
		Banner banner = new Banner();
		banner.setFlag(1);
		banner.setObjId(3);
		banner.setObjName("郑州市");
		banner.setCreateName("admin");
		banner.setCreateTime(createTime);
		banner.setName("郑州市banner");
		banner.setStatus(1);
		bannerDao.add(banner);

		Pic pic1 = new Pic();
		pic1.setBid(banner.getId());
		pic1.setCreateName("admin");
		pic1.setCreateTime(createTime);
		pic1.setName("2015夏令营...");
		pic1.setPath("zhengzhou001.png");
		picDao.add(pic1);

		Pic pic2 = new Pic();
		pic2.setBid(banner.getId());
		pic2.setCreateName("admin");
		pic2.setCreateTime(createTime);
		pic2.setName("少儿拉丁舞大赛...");
		pic2.setPath("zhengzhou002.png");
		picDao.add(pic2);

		Pic pic3 = new Pic();
		pic3.setBid(banner.getId());
		pic3.setCreateName("admin");
		pic3.setCreateTime(createTime);
		pic3.setName("少儿英语...");
		pic3.setPath("zhengzhou003.png");
		picDao.add(pic3);

		return new AjaxObj(1, "行政区域初始化完毕！");
	}

	/** 初始化BUG管理 */
	private AjaxObj addBugs() {
		String createTime = DateFormat.dateToString(new Date());

		BugType bugType1 = new BugType();
		bugType1.setCreateName("admin");
		bugType1.setCreateTime(createTime);
		bugType1.setName("功能性BUG");
		bugType1.setOrderIndex(99);
		bugTypeDao.add(bugType1);

		BugType bugType2 = new BugType();
		bugType2.setCreateName("admin");
		bugType2.setCreateTime(createTime);
		bugType2.setName("兼容性BUG");
		bugType2.setOrderIndex(10);
		bugTypeDao.add(bugType2);
		return new AjaxObj(1, "BUG类型初始化完毕");
	}

}
